#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <unistd.h>

#include "common/surface_utils.h"
#include "common/b_spline3.h"
#include "common/image_cache.h"

struct MyRange : Range{
	void getRange(unsigned char height,std::vector<float>& result)const{
		static float reference[4] = {255,191,127,63};

		result.clear();
		for(unsigned int c=0;c<4;c++){
			float percent;
			percent = (64.0f - fabs(reference[c] - height)) / 64.0f;

			if(percent < 0.0f) percent = 0.0f;
			else if(percent > 1.0f) percent = 1.0f;

			result.push_back(percent);
		}

		if(height<63){
			result.pop_back();
			result.push_back(1.0f);
		}
	}
};

void printHelp();

int main(int argc,char** argv){
	ImageCache::TexturePtr result = ImageCache::TexturePtr(new Texture());
	ImageCache::TexturePtr hmap = ImageCache::instance().load("/home/HPK/Escritorio/","heightmap.png");
	std::vector<ImageCache::TexturePtr> textures;
	std::vector<const Texture*> textures_ptrs;
	textures.push_back(ImageCache::instance().load("/home/HPK/Escritorio/","dirty-grass.png"));
	textures.push_back(ImageCache::instance().load("/home/HPK/Escritorio/","basematerial.png"));
	textures.push_back(ImageCache::instance().load("/home/HPK/Escritorio/","basematerial.png"));
	textures.push_back(ImageCache::instance().load("/home/HPK/Escritorio/","green-grass.png"));

	textures_ptrs.push_back(&*textures[0]);
	textures_ptrs.push_back(&*textures[1]);
	textures_ptrs.push_back(&*textures[2]);
	textures_ptrs.push_back(&*textures[3]);

	MyRange r;
	SurfaceUtils::instance().generateHeightMapTexture(textures_ptrs,*result,*hmap,r);

	ImageCache::instance().save("/home/HPK/Escritorio/",result);

	return 0;
}

void printHelp(){
}
